﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class ExamTypeEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ExamType examType = ctx.ExamTypes.Single(r => r.Id ==int.Parse(id));
                    Title.Value = examType.Title;
                    StartTime.Value = examType.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
                    EndTime.Value = examType.EndTime.ToString("yyyy-MM-dd HH:mm:ss");
                    Greetings.Value = examType.Greetings;
                    Conclusion.Value = examType.Conclusion;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string id = Request.QueryString["id"];
            ExamType examType = new ExamType();
            if (!string.IsNullOrEmpty(id))
            {
                examType = ctx.ExamTypes.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                ctx.ExamTypes.InsertOnSubmit(examType);
            }
            examType.Title = Title.Value;
            examType.StartTime = DateTime.Parse(StartTime.Value);
            examType.EndTime = DateTime.Parse(EndTime.Value);
            examType.Greetings = Greetings.Value;
            examType.Conclusion = Conclusion.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}